package org.hackystat.devcathlon.ui.page.scoreboard.halloffame;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.hackystat.devcathlon.ui.page.DevcathlonBasePage;

/**
 * This page represents the submenu of Hall of Fame.
 * 
 * The "Hall of Fame" submenu should provide information about the "best" matches, teams, and 
 * individuals in the system, along with drill-downs for more details. 
 * 
 * @author Robin Raqueno
 *
 */

public class HallofFamePage extends DevcathlonBasePage {
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Creates the Hall of fame page.  
   */
  public HallofFamePage() {
    
    // Set the body class attribute to section-1 so that the menubar is correct.
    add(new WebMarkupContainer("body") { 
      private static final long serialVersionUID = 1L;
      @Override
      public boolean isTransparentResolver() { 
        return true; 
        } 
      @Override
      protected void onComponentTag(ComponentTag tag) { 
        tag.put("class","section-4"); 
        }
    }
    );
/*    
    final HashMap<String, Integer> mapMostTeamWins = new HashMap<String, Integer>();
    final HashMap<String, Integer> mapMostTeamPoints = new HashMap<String, Integer>();
    final HashMap<String, Integer> mapMostDeveloperPoints = new HashMap<String, Integer>();
    final HashMap<String, Integer> mapMostPopularEvents = new HashMap<String, Integer>();
    
    Collection<Match> matches = MatchManager.getInstance().getAllMatches();
    
    
    for (Match match : matches) {
      
      List<Event> events = match.getEvents();
      for (Event event : events) {
        String eventName = event.getName();
        int eventCount = 1;
        if (mapMostPopularEvents.containsKey(eventName)) {
          eventCount += mapMostPopularEvents.get(eventName).intValue();
        }
        mapMostPopularEvents.put(eventName, Integer.valueOf(eventCount));
      }
      
      // Most Team Points, Most Team Wins, Most Developer Points
      Set<Team> teams = match.getTeams();
      Team winner = null;
      int highScore = 0;
      for (Team team : teams) {
        for (User user : team.getMembers()) {
          int currentUserPoints = match.getTotalScore(user);
          if (mapMostDeveloperPoints.containsKey(user.getEmail())) {
            Integer points = mapMostDeveloperPoints.get(user.getEmail());
            currentUserPoints += points.intValue();
          }
          mapMostDeveloperPoints.put(user.getEmail(), Integer.valueOf(currentUserPoints));
        }
        User owner = team.getOwner();
        int currentUserPoints = match.getTotalScore(owner);
        if (mapMostDeveloperPoints.containsKey(owner.getEmail())) {
        Integer points = mapMostDeveloperPoints.get(owner.getEmail());
          currentUserPoints += points.intValue();
        }
        mapMostDeveloperPoints.put(owner.getEmail(), Integer.valueOf(currentUserPoints));
        
        if (!mapMostTeamWins.containsKey(team.getName())) {
          mapMostTeamWins.put(team.getName(), Integer.valueOf(0));
        }
        int totalScore = match.getTotalScore(team);
        if (winner == null) {
          winner = team;
          highScore = totalScore;
        }
        else if (highScore < totalScore) {
          winner = team;
          highScore = totalScore;
        }
        else if (highScore == totalScore) {
          winner = null;
        }
        mapMostTeamPoints.put(team.getName() + "(" + match.getName() + ")", 
                              Integer.valueOf(totalScore));
      }
      if (winner != null) {
        Integer wins = mapMostTeamWins.get(winner.getName());
        mapMostTeamWins.put(winner.getName(), Integer.valueOf(wins.intValue() + 1));
      }
    } 
    
    // Most Team Wins
    final List<String> listMostTeamWins = new ArrayList<String>();
    Set<Map.Entry<String,Integer>> mapentryMapMostTeamWins = mapMostTeamWins.entrySet();
    while (!mapentryMapMostTeamWins.isEmpty()) {
      Map.Entry<String, Integer> most = null;
      for (Map.Entry<String,Integer> mapentry : mapentryMapMostTeamWins) {
        if (most == null) {
          most = mapentry;
        }
        if (most.getValue().intValue() < mapentry.getValue().intValue()) {
          most = mapentry;
        }
      }
      listMostTeamWins.add(most.getKey() + " " + most.getValue().toString() + " wins");
      mapentryMapMostTeamWins.remove(most);
    }
    Loop loopMostTeamWins = new Loop("listMostTeamWins", listMostTeamWins.size()) {
      /** For serialization. */
/*      private static final long serialVersionUID = 1L;

      protected void populateItem(LoopItem item) {
        item.add(new FragmentLabel("panelMostTeamWins", "fragmentMostTeamWins", this, 
                                   "labelMostTeamWins", 
                                   listMostTeamWins.get(item.getIteration())));
      }
    };
    add(loopMostTeamWins);
    
    final List<String> listMostTeamPoints = new ArrayList<String>();
    Set<Map.Entry<String,Integer>> mapentryMapMostTeamPoints = mapMostTeamPoints.entrySet();
    while (!mapentryMapMostTeamPoints.isEmpty()) {
      Map.Entry<String, Integer> most = null;
      for (Map.Entry<String,Integer> mapentry : mapentryMapMostTeamPoints) {
        if (most == null) {
          most = mapentry;
        }
        if (most.getValue().intValue() < mapentry.getValue().intValue()) {
          most = mapentry;
        }
      }
      listMostTeamPoints.add(most.getKey() + " " + most.getValue().toString() + " points");
      mapentryMapMostTeamPoints.remove(most);
    }
    Loop loopMostTeamPoints = new Loop("listMostTeamPoints", listMostTeamPoints.size()) {
      /** For serialization. */
/*      private static final long serialVersionUID = 1L;

      protected void populateItem(LoopItem item) {
        String fragmentId = "fragmentMostTeamPoints";
        item.add(new FragmentLabel("panelMostTeamPoints", fragmentId, this, 
                                   "labelMostTeamPoints",
                                   listMostTeamPoints.get(item.getIteration())));
      }
    };
    add(loopMostTeamPoints);
    
    final List<String> listMostDeveloperPoints = new ArrayList<String>();
    Set<Map.Entry<String,Integer>> mapentryMapMostDeveloperPoints = 
                                   mapMostDeveloperPoints.entrySet();
    while (!mapentryMapMostDeveloperPoints.isEmpty()) {
      Map.Entry<String, Integer> most = null;
      for (Map.Entry<String,Integer> mapentry : mapentryMapMostDeveloperPoints) {
        if (most == null) {
          most = mapentry;
        }
        if (most.getValue().intValue() < mapentry.getValue().intValue()) {
          most = mapentry;
        }
      }
      listMostDeveloperPoints.add(most.getKey() + " " + most.getValue().toString() + " pts");
      mapentryMapMostDeveloperPoints.remove(most);
    }
    Loop loopMostDeveloperPoints = new Loop("listMostDeveloperPoints",
                                            listMostDeveloperPoints.size()) {
      /** For serialization. */
/*      private static final long serialVersionUID = 1L;

      protected void populateItem(LoopItem item) {
        String fragmentId = "fragmentMostDeveloperPoints";
        item.add(new FragmentLabel("panelMostDeveloperPoints", fragmentId, this,
                                   "labelMostDeveloperPoints",
                                   listMostDeveloperPoints.get(item.getIteration())));
      }
    };
    add(loopMostDeveloperPoints);
    
    final List<String> listMostPopularEvents = new ArrayList<String>();
    Set<Map.Entry<String,Integer>> mapentryMapMostPopularEvents = 
                                   mapMostPopularEvents.entrySet();
    while (!mapentryMapMostPopularEvents.isEmpty()) {
      Map.Entry<String, Integer> most = null;
      for (Map.Entry<String,Integer> mapentry : mapentryMapMostPopularEvents) {
        if (most == null) {
          most = mapentry;
        }
        if (most.getValue().intValue() < mapentry.getValue().intValue()) {
          most = mapentry;
        }
      }
      listMostPopularEvents.add(most.getKey() + " (" + most.getValue().toString() + " count)");
      mapentryMapMostPopularEvents.remove(most);
    }
    Loop loopMostPopularEvents = new Loop("listMostPopularEvents", listMostPopularEvents.size()) {
      /** For serialization. */
/*      private static final long serialVersionUID = 1L;

      protected void populateItem(LoopItem item) {
        String fragmentId = "fragmentMostPopularEvents";
        item.add(new FragmentLabel("panelMostPopularEvents", fragmentId, this,
                           "labelMostPopularEvents",
                           listMostPopularEvents.get(item.getIteration())));
      }
    };
    add(loopMostPopularEvents);

    // Make the tab corresponding to this page bold.
    get("HallofFameLink").add(new SimpleAttributeModifier("class", "selected"));
*/
    add(new MostPopularEventsPanel ("mostPopularEvents_panel"));
    add(new MostDeveloperPointsPanel ("mostDeveloperPoints_panel"));
    add(new MostTeamPointsPanel ("mostTeamPoints_panel"));
    add(new MostTeamWinsPanel ("mostTeamWins_panel"));

    
  }
}
